/**
 *
 */
package steven.location;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * @author steven.lam.t.f
 *
 */
public class LocationPlugin implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
	private final Context context;
	private final GoogleApiClient client;
	private final LocationRequest locationRequest;
	private final LocationListener locationListener;

	public LocationPlugin(final Context context, final LocationListener locationListener){
		this.context = context;
		this.client = new GoogleApiClient.Builder(context).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
		this.locationRequest = new LocationRequest();
		this.locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
		this.locationRequest.setFastestInterval(1000);
		this.locationRequest.setInterval(1000);
		this.locationRequest.setSmallestDisplacement(0);
		this.locationListener = locationListener;
	}
	@Override
	public void onConnectionFailed(final ConnectionResult result){
		if(result.hasResolution()){
			Log.e("steven", "onConnectionFailed " + result);
			//result.startResolutionForResult(activity, requestCode);
		}
	}
	@Override
	public void onConnected(final Bundle connectionHint){
		Log.e("steven", "onConnected " + connectionHint);
		if(this.locationListener == null){
			try{
				this.setMockMode(true);
			}catch(final Exception e){
				Toast.makeText(this.context, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
			}
		}else{
			LocationServices.FusedLocationApi.requestLocationUpdates(this.client, this.locationRequest, this.locationListener);
		}
	}
	@Override
	public void onConnectionSuspended(final int cause){
	}
	public void start(){
		this.client.connect();
	}
	public void stop(){
		if(this.locationListener == null){
			try{
				this.setMockMode(false);
			}catch(final Exception e){
				Toast.makeText(this.context, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
			}
		}else{
			LocationServices.FusedLocationApi.removeLocationUpdates(this.client, this.locationListener);
		}
		this.client.disconnect();
	}
	public void setMockMode(final boolean isMockMode){
		//SecureUtils.setMockLocationAllowed(this.context, true);
		LocationServices.FusedLocationApi.setMockMode(this.client, isMockMode);
		//SecureUtils.setMockLocationAllowed(this.context, false);
	}
	public void setLocation(final Location location){
		//SecureUtils.setMockLocationAllowed(this.context, true);
		LocationServices.FusedLocationApi.setMockLocation(this.client, location);
		//SecureUtils.setMockLocationAllowed(this.context, false);
	}
}
